<?php
/**
 * provides protlets' info for welcome panel
 * @package    
 * @author     ben
 */
class getPortletsInfoAction extends sfAction
{
    public function execute($request)
    {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();

        if(sfContext::getInstance()->getUser()->isAuthenticated())
        {
            $dateTimeCalculator = new DateTimeCalculator(date("Y-m-d"), "Y-m-d");
            $defaultAlertDays = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$defaultAlertDeliveryDays);
            $date = $dateTimeCalculator->add("d", $defaultAlertDays);
            $defaultRows = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$defaultAlertRows);

            $params = array(
                "minDate" => date("Y-m-d"),
                "maxDate" => $date,
                "status" => StaticWacFormStatus::$init
            );

            if($this->getUser()->hasCredential("app_customer_order_audit")) {
                $params["status"] = StaticWacFormStatus::$audited;
            }
            $resultSet['items']['customerOrders'] = Doctrine::getTable(WacTable::$customerOrder)->getListByDeliveryDate($params, 1, $defaultRows);

            if($this->getUser()->hasCredential("app_production_order_audit")) {
                $params["status"] = StaticWacFormStatus::$audited;
            }
            $resultSet['items']['productionOrders'] = Doctrine::getTable(WacTable::$productionOrder)->getListByDeliveryDate($params, 1, $defaultRows);

            if($this->getUser()->hasCredential("app_weave_order_audit")) {
                $params["status"] = StaticWacFormStatus::$audited;
            }
            $resultSet['items']['weaveOrders'] = Doctrine::getTable(WacTable::$weaveOrder)->getListByDeliveryDate($params, 1, $defaultRows);

            if($this->getUser()->hasCredential("app_final_cloth_form_audit")) {
                $params["status"] = StaticWacFormStatus::$audited;
            }
            $resultSet['items']['finalClothForms'] = Doctrine::getTable(WacTable::$finalClothForm)->getListByDeliveryDate($params, 1, $defaultRows);

            $resultSet['info'] = JsCommonData::getSuccessDatum();
        }
        else
        {
            $resultSet['info'] = JsCommonData::getErrorDatum(
                       Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_invalid_user_authenticate"),  WacErrorCode::$logicError
                    );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }
}